package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.domain.dto.request.LoginRequest;
import com.example.domain.dto.request.RegisterRequest;
import com.example.domain.dto.result.ApiResponse;
import com.example.domain.dto.result.PersonResult;
import com.example.domain.entity.Person;
import com.example.domain.entity.Product;
import com.example.domain.query.PersonQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.query.ProductQuery;
import com.example.factory.ResultFactory;
import com.example.service.IPersonService;
import com.example.service.impl.PersonServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2025-05-12
 */
@CrossOrigin
@RestController
@RequestMapping("/person")
@Tag(name = "人员管理器")
public class PersonController {
    @Autowired
    private PersonServiceImpl personService;

    @Autowired
    private ResultFactory resultFactory;

    @GetMapping("/list")
    @Operation(summary = "获取人员列表")
    public List<PersonResult> list() {
        List<Person> personList = personService.list();
        return personList.stream().map(resultFactory::convert).toList();
    }

    @GetMapping("/getByDept")
    @Operation(summary = "根据部门获取人员列表")
    public List<PersonResult> getByDept(Integer deptId) {
        QueryWrapper<Person> wrapper = new QueryWrapper<Person>().eq("dept_id", deptId);
        List<Person> personList = personService.list(wrapper);
        return personList.stream().map(resultFactory::convert).toList();
    }

    @PostMapping("/save")
    @Operation(summary = "添加人员")
    public boolean save(@RequestBody Person person) {
        person.setPassword("1234");
        return personService.save(person);
    }

    @PostMapping("/update")
    @Operation(summary = "更新人员")
    public boolean update(@RequestBody Person person) {
        return personService.updateById(person);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除人员")
    public void deleteById(@PathVariable int id) {
        if (!personService.removeById(id)) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "删除失败，人员不存在");
        }
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询人员")
    public IPage<PersonResult> pageQuery(@RequestBody PersonQuery query,
                                    @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        return resultFactory.convertPersonPage(this.personService.pageQuery(query, pageNum, pageSize));
    }

    @GetMapping("/schedule")
    @Operation(summary = "安排人员")
    public boolean schedule(Integer personId, Integer deptId) {
        return this.personService.schedule(personId, deptId);
    }
}
